package org.example.five;

import com.rabbitmq.client.*;
import org.example.utils.RabbitMqUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ReceiveLogs02 {
    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtil.getChannel();
        //声明一个交换机
        //fanout:扇出交换机,发布订阅交换机,一个生产者发布,交换机中所有队列都能接收到消息
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
        //声明一个队列,临时队列,连接一关闭就会自动消失的队列
        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机与队列
        channel.queueBind(queueName,EXCHANGE_NAME,"");
        DeliverCallback deliverCallback = (String message, Delivery delivery) -> {
            System.out.println("ReceiveLogs02消息接收到了");
            System.out.println(new String(delivery.getBody()));
        };
        CancelCallback cancelCallback = message -> {
            System.out.println("消息取消了");
            System.out.println(message);
        };
        channel.basicConsume(queueName,false,deliverCallback,cancelCallback);

    }
}
